<template>
  <div class="register-bg">
    <div class="register-container">
      <div class="login-left">
        <div class="login-left-content">
          <h2>Welcome to</h2>
          <h1>招标文件智析系统</h1>
          <p>你的强有力招标文件分析工具</p>
        </div>
      </div>
      <div class="login-right">
        <h2>REGISTER</h2>
        <form @submit.prevent="handleRegister">
          <div v-if="showAccountError" class="error-tip">此账号已存在</div>
          <div v-if="registerSuccess" class="success-tip">{{ successMsg }}</div>
          <input
            type="text"
            v-model="phone"
            placeholder="请输入手机号"
            @focus="triggerPhoneTip()"
            @input="triggerPhoneTip()"
          />
          <div v-if="phone && phoneInvalid" style="color: #000; font-size: 0.95rem; margin-bottom: 8px;">
            手机号不正确
          </div>
          <div v-if="showPhoneError" style="color: red; font-size: 0.95rem; margin-bottom: 8px;">
            {{ phoneErrorMsg }}
          </div>
          <div class="verify-box">
            <input type="text" v-model="code" placeholder="请输入验证码" @input="onCodeInput" />
            <button type="button" class="verify-btn" @click="getCode" :disabled="countdown > 0">
              <span v-if="countdown === 0">获取验证码</span>
              <span v-else>{{ countdown }}秒后重试</span>
            </button>
            <span v-if="codeSent && countdown > 0" class="code-tip">验证码已发送</span>
          </div>
          <div v-if="showCodeTip" style="color: #000; font-size: 0.95rem; margin-bottom: 8px;">
            {{ codeTipMsg }}
          </div>
          <div class="password-box">
            <input :type="showPassword ? 'text' : 'password'" v-model="password" placeholder="请输入密码" @input="passwordTouched = true" />
            <span class="toggle" @click="showPassword = !showPassword">
              <img 
                :src="showPassword ? eyeShowIcon : eyeHideIcon" 
                alt="toggle password"
                class="eye-icon"
              />
            </span>
          </div>
          <div v-if="passwordTouched && passwordInvalid" style="color: #000; font-size: 0.85rem; margin-bottom: 8px;">
            密码至少6位，且必须包含字母和数字
          </div>
          <div class="password-box">
            <input :type="showConfirmPassword ? 'text' : 'password'" v-model="confirmPassword" placeholder="再次输入密码" @input="confirmTouched = true" />
            <span class="toggle" @click="showConfirmPassword = !showConfirmPassword">
              <img 
                :src="showConfirmPassword ? eyeShowIcon : eyeHideIcon" 
                alt="toggle confirm password"
                class="eye-icon"
              />
            </span>
          </div>
          <div v-if="confirmTouched && confirmPassword && confirmPassword !== password" style="color: #000; font-size: 0.95rem; margin-bottom: 8px;">
            两次输入的密码不一致
          </div>
          <div class="agreement">
            注册即代表同意
            <a href="#">《用户协议》</a>
            和
            <a href="#">《隐私协议》</a>
          </div>
          <button type="submit">注册</button>
        </form>
        <div class="login-footer">
          <span></span>
          <router-link to="/login">已有账号？去登录！</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import eyeShowIcon from '@/assets/icons/眼睛_显示.svg';
import eyeHideIcon from '@/assets/icons/眼睛_隐藏.svg';

export default {
  name: "RegisterPage",
  data() {
    return {
      phone: "",
      code: "",
      password: "",
      confirmPassword: "",
      showPassword: false,
      showConfirmPassword: false,
      eyeShowIcon,
      eyeHideIcon,
      sentCode: "",
      phoneTouched: false,
      passwordTouched: false,
      confirmTouched: false,
      codeTouched: false,
      codeError: false,
      codeSent: false,
      countdown: 0,
      timer: null,
      registerSuccess: false,
      successMsg: "",
      accountExists: false,
      showPhoneError: false,
      showPasswordError: false,
      showCodeError: false,
      showAccountError: false,
      phoneErrorMsg: "",
      codeErrorMsg: "",
      showPhoneTip: false,
      phoneTipMsg: '',
      phoneTipTimer: null,
      showCodeTip: false,
      codeTipMsg: '',
      codeTipTimer: null,
      showPasswordTip: false,
      passwordTipMsg: '',
      passwordTipTimer: null,
      showConfirmTip: false,
      confirmTipMsg: '',
      confirmTipTimer: null,
    };
  },
  computed: {
    phoneInvalid() {
      if (!this.phoneTouched) return false;
      const phoneReg = /^1[3-9]\d{9}$/;
      return !phoneReg.test(this.phone);
    },
    passwordInvalid() {
      if (!this.passwordTouched) return false;
      const pwdReg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/;
      return !pwdReg.test(this.password);
    },
  },
  methods: {
    showError(type, message) {
      // 隐藏所有错误提示
      this.showPhoneError = false;
      this.showPasswordError = false;
      this.showCodeError = false;
      this.showAccountError = false;
      
      // 显示指定类型的错误提示
      switch(type) {
        case 'phone':
          this.showPhoneError = true;
          this.phoneErrorMsg = message;
          break;
        case 'password':
          this.showPasswordError = true;
          break;
        case 'code':
          this.showCodeError = true;
          this.codeErrorMsg = message;
          break;
        case 'account':
          this.showAccountError = true;
          break;
      }
      
      // 2秒后自动隐藏
      setTimeout(() => {
        this.showPhoneError = false;
        this.showPasswordError = false;
        this.showCodeError = false;
        this.showAccountError = false;
      }, 2000);
    },
    onCodeInput() {
      this.codeTouched = true;
      this.codeError = false;
      this.accountExists = false;
    },
    getCode() {
      if (!this.phone) {
        this.triggerPhoneTip('请输入账号');
        return;
      }
      const phoneReg = /^1[3-9]\d{9}$/;
      if (!phoneReg.test(this.phone)) {
        this.triggerPhoneTip('手机号不正确');
        return;
      }
      const code = Math.floor(100000 + Math.random() * 900000).toString();
      this.sentCode = code;
      this.code = code;
      this.codeSent = true;
      this.countdown = 60;
      if (this.timer) clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.countdown--;
        if (this.countdown <= 0) {
          clearInterval(this.timer);
          this.timer = null;
          this.codeSent = false;
        }
      }, 1000);
    },
    handleRegister() {
      // 检查手机号
      if (!this.phone) {
        this.triggerPhoneTip('请输入手机号');
        return;
      }
      
      const phoneReg = /^1[3-9]\d{9}$/;
      if (!phoneReg.test(this.phone)) {
        this.triggerPhoneTip('手机号格式不正确');
        return;
      }
      
      // 检查验证码
      if (!this.code) {
        this.triggerCodeTip('请输入验证码');
        return;
      }
      
      if (this.code !== this.sentCode) {
        this.triggerCodeTip('验证码错误');
        return;
      }
      
      // 检查密码
      if (!this.password) {
        this.triggerPasswordTip('请输入密码');
        return;
      }
      
      const pwdReg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/;
      if (!pwdReg.test(this.password)) {
        this.triggerPasswordTip('密码至少6位，且必须包含字母和数字');
        return;
      }
      
      // 检查两次密码是否一致
      if (!this.confirmPassword) {
        this.triggerConfirmTip('请确认密码');
        return;
      }
      
      if (this.password !== this.confirmPassword) {
        this.triggerConfirmTip('两次输入的密码不一致');
        return;
      }
      
      // 检查手机号是否已注册
      let registeredPhones = JSON.parse(localStorage.getItem('registeredPhones') || '[]');
      if (registeredPhones.includes(this.phone)) {
        this.showAccountError = true;
        return;
      }
      
      // 存储注册信息
      registeredPhones.push(this.phone);
      localStorage.setItem('registeredPhones', JSON.stringify(registeredPhones));
      
      // 存储密码
      const userPasswords = JSON.parse(localStorage.getItem('userPasswords') || '{}');
      userPasswords[this.phone] = this.password;
      localStorage.setItem('userPasswords', JSON.stringify(userPasswords));
      
      // 注册成功
      this.registerSuccess = true;
      this.successMsg = "注册成功，即将跳转到登录页！";
      
      // 2秒后跳转到登录页
      setTimeout(() => {
        this.$router.push('/login');
      }, 2000);
    },
    triggerPhoneTip(msg) {
      if (this.phoneTipTimer) clearTimeout(this.phoneTipTimer);
      this.phoneTipMsg = msg;
      this.showPhoneTip = true;
      this.phoneTipTimer = setTimeout(() => {
        this.showPhoneTip = false;
      }, 2000);
    },
    triggerCodeTip(msg) {
      if (this.codeTipTimer) clearTimeout(this.codeTipTimer);
      this.codeTipMsg = msg;
      this.showCodeTip = true;
      this.codeTipTimer = setTimeout(() => {
        this.showCodeTip = false;
      }, 2000);
    },
    triggerPasswordTip(msg) {
      if (this.passwordTipTimer) clearTimeout(this.passwordTipTimer);
      this.passwordTipMsg = msg;
      this.showPasswordTip = true;
      this.passwordTipTimer = setTimeout(() => {
        this.showPasswordTip = false;
      }, 2000);
    },
    triggerConfirmTip(msg) {
      if (this.confirmTipTimer) clearTimeout(this.confirmTipTimer);
      this.confirmTipMsg = msg;
      this.showConfirmTip = true;
      this.confirmTipTimer = setTimeout(() => {
        this.showConfirmTip = false;
      }, 2000);
    },
  },
  beforeUnmount() {
    if (this.timer) clearInterval(this.timer);
  }
};
</script>

<style scoped>
.register-bg {
  min-height: 100vh;
  background: url('../assets/img/background.png') no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.register-bg::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(5px);
  z-index: 1;
}

.register-container {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-top: -80px;
  margin-left: -80px;
  z-index: 2;
}

.login-left, .login-right {
  min-height: unset;
  height: auto;
  overflow: hidden;
}

.login-left {
  background: #5a6b7f;
  color: #fff;
  border-radius: 80px;
  padding: 60px 60px 60px 60px;
  width: 900px;
  height: 380px;
  margin-right: -120px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  display: flex;
  flex-direction: column;
  justify-content: center;
  z-index: 1;
  position: relative;
}

.login-left-content {
  text-align: left;
}

.login-left h2 {
  font-size: 3rem;
  margin-bottom: 22px;
  font-weight: 700;
}
.login-left h1 {
  font-size: 3.2rem;
  margin-bottom: 22px;
  font-weight: 700;
}
.login-left p {
  font-size: 1.2rem;
  opacity: 0.9;
}
.login-right {
  background: #d4c4a8;
  border-radius: 80px;
  padding: 60px 40px;
  width: 320px;
  height: 580px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
  position: relative;
  margin-left: -380px;
}
.login-right h2 {
  text-align: center;
  margin-bottom: 40px;
  font-size: 2.3rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: 2px;
}
form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
input[type="text"], input[type="password"] {
  width: 90%;
  padding: 13px 16px;
  margin-bottom: 0;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  background: #fff;
  outline: none;
  display: block;
  margin-left: auto;
  margin-right: auto;
}
.verify-box {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 0;
}
.verify-box input[type="text"] {
  flex: 1;
  margin-bottom: 0;
  margin-right: 12px;
}
.verify-btn {
  padding: 0 18px;
  height: 40px;
  background: #7b90a7;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s;
}
.verify-btn:hover {
  background: #264477;
}
.password-box {
  position: relative;
}
.password-box .toggle {
  position: absolute;
  right: 12px;
  top: 12px;
  cursor: pointer;
  font-size: 1.1rem;
}
.agreement {
  font-size: 0.95rem;
  color: #888;
  margin-bottom: 18px;
}
.agreement a {
  color: #4a7eb3;
  text-decoration: none;
  margin: 0 2px;
}
button[type="submit"] {
  width: 90%;
  display: block;
  margin: 0 auto 0 auto;
  padding: 13px 0;
  background: #7b90a7;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1.1rem;
  cursor: pointer;
  margin-bottom: 0;
  transition: background 0.2s;
}
button[type="submit"]:hover {
  background: #264477;
}
.login-footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  font-size: 1rem;
  color: #888;
  margin-top: 8px;
}
.login-footer a {
  color: #2b649e;
  text-decoration: none;
}
@media (max-width: 1300px) {
  .register-container {
    min-width: 100vw;
    flex-direction: column;
    align-items: center;
  }
  .login-left, .login-right {
    position: static !important;
    width: 90vw !important;
    min-width: unset;
    margin: 0 auto 24px auto !important;
    left: unset !important;
    top: unset !important;
    height: auto !important;
  }
}
.code-tip {
  color: #2b649e;
  font-size: 0.95rem;
  margin-left: 12px;
}
.success-tip {
  color: #2ecc40;
  background: #eafbe7;
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 16px;
  font-size: 1.1rem;
  text-align: center;
}
.error-tip {
  color: #e74c3c;
  background: #fbeaea;
  border-radius: 6px;
  padding: 12px;
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 1rem;
  text-align: center;
}
input[type="password"]::-ms-reveal,
input[type="password"]::-ms-clear {
  display: none;
}
input[type="password"]::-webkit-credentials-auto-fill-button,
input[type="password"]::-webkit-input-password-toggle-button,
input[type="password"]::-webkit-input-password-toggle {
  display: none !important;
}

.eye-icon {
  width: 20px;
  height: 20px;
  cursor: pointer;
  transition: opacity 0.2s ease;
}

.eye-icon:hover {
  opacity: 0.7;
}
</style>
